package com.zlfund.cat.zjgsmk.widget;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.zlfund.cat.zjgsmk.R;
import com.zlfund.cat.zjgsmk.ui.base.ZApplication;
import com.zlfund.cat.zjgsmk.util.GlideUtil;

/**
 * @author chenj
 * @time 2016.4.25
 */
public class RemindDialog extends Dialog {

    private TextView tvContent;
    private Button btnCancel, btnSubmit;
    private OnCancelListener mCancelListener;
    private OnSubmitListener mSubmitListener;
    private TextView tvSubContent;
    private ImageView ivIcon;
    private Context mContext;

    private RemindDialog(Context context) {
        this(context, R.style.dialogStyle);
    }

    private RemindDialog(Context context, int theme) {
        super(context, theme);
        mContext = context;
        init();
    }

    @Override
    public void show() {
        super.show();
        try {
            setOwnerActivity((Activity) getContext());
        } catch (Exception e) {

        }
        Window window = getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = (int) (ZApplication.SCREEN_WIDTH * 0.75);
        window.setAttributes(lp);
    }

    private void init() {
        setContentView(R.layout.dialog_remind);

        tvContent = (TextView) findViewById(R.id.tv_content);
        btnCancel = (Button) findViewById(R.id.btn_cancel);
        btnSubmit = (Button) findViewById(R.id.btn_submit);
        tvSubContent = (TextView) findViewById(R.id.tv_sub_content);
        ivIcon = (ImageView) findViewById(R.id.iv_icon);

        btnSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mSubmitListener != null) {
                    mSubmitListener.onSubmit(v);
                }
                dismiss();
            }
        });
        btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mCancelListener != null) {
                    mCancelListener.onCancel(v);
                }
                dismiss();
            }
        });
    }

    public static class Build {
        private RemindDialog dialog;

        public Build(Context context) {
            dialog = new RemindDialog(context);
        }

        public Build setMessage(CharSequence message) {
            if (message instanceof String) {
                String msg = message.toString();
                if (msg.contains("[") && msg.contains("]")) {
                    int index1 = msg.indexOf("[");
                    int index2 = msg.indexOf("]");
                    msg = msg.substring(0, index1) + msg.substring(index2 + 1, msg.length());
                }
                dialog.tvContent.setText(msg);
            }else {
                dialog.tvContent.setText(message);
            }
            return this;
        }

        public Build setSubContent(CharSequence message) {
            dialog.tvSubContent.setText(message);
            dialog.tvSubContent.setVisibility(View.VISIBLE);
            return this;
        }

        public Build setIcon(int resId) {
            if (resId != 0) {
                GlideUtil.loadRes(dialog.mContext, resId, dialog.ivIcon);
            }
            dialog.ivIcon.setVisibility(View.VISIBLE);
            return this;
        }

        public Build setCancelText(CharSequence cancelText) {
            if (!TextUtils.isEmpty(cancelText.toString())) {
                dialog.btnCancel.setText(cancelText);
            }
            return this;
        }

        public Build setSubmitText(CharSequence submitText) {
            if (!TextUtils.isEmpty(submitText.toString())) {
                dialog.btnSubmit.setText(submitText);
            }
            return this;
        }

        public Build setSubmitTextColor(int color){
            dialog.btnSubmit.setTextColor(color);
            return this;
        }

        public Build setOnCancelListener(OnCancelListener listener) {
            dialog.mCancelListener = listener;
            return this;
        }

        public Build hideTwoViews() {
            dialog.tvSubContent.setVisibility(View.GONE);
            dialog.ivIcon.setVisibility(View.GONE);
            return this;
        }

        public Build showOneBtn() {
            dialog.btnCancel.setVisibility(View.GONE);
            dialog.btnSubmit.setBackgroundResource(R.drawable.shape_bottom_right);
            return this;
        }

        public Build showTwoBtn() {
            dialog.btnCancel.setVisibility(View.VISIBLE);
            return this;
        }

        public Build setOnSubmitListener(OnSubmitListener listener) {
            dialog.mSubmitListener = listener;
            return this;
        }

        public RemindDialog show() {
            dialog.show();
            return dialog;
        }

        public RemindDialog create() {
            return dialog;
        }

    }

    public interface OnCancelListener {
        void onCancel(View v);
    }

    public interface OnSubmitListener {
        void onSubmit(View v);
    }

}